---
sidebar_label: Perplexity
title: ChatPerplexity
---

export const quartoRawHtml = [
  `
<table style="width:100%;">
<colgroup>
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 15%" />
<col style="width: 15%" />
<col style="width: 15%" />
<col style="width: 15%" />
<col style="width: 15%" />
</colgroup>
<thead>
<tr>
<th style="text-align: left;">Class</th>
<th style="text-align: left;">Package</th>
<th style="text-align: center;">Local</th>
<th style="text-align: center;">Serializable</th>
<th style="text-align: center;"><a href="https://python.langchain.com/docs/integrations/chat/perplexity/">PY support</a></th>
<th style="text-align: center;">Package downloads</th>
<th style="text-align: center;">Package latest</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;"><a href="https://api.js.langchain.com/classes/_langchain_community.chat_models_perplexity.ChatPerplexity.html"><code>ChatPerplexity</code></a></td>
<td style="text-align: left;"><a href="https://npmjs.com/@langchain/community"><code>@langchain/community</code></a></td>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">beta</td>
<td style="text-align: center;">✅</td>
<td style="text-align: center;"><img src="https://img.shields.io/npm/dm/@langchain/community?style=flat-square&amp;label=%20&amp;.png" alt="NPM - Downloads" /></td>
<td style="text-align: center;"><img src="https://img.shields.io/npm/v/@langchain/community?style=flat-square&amp;label=%20&amp;.png" alt="NPM - Version" /></td>
</tr>
</tbody>
</table>
`,
  `
<table style="width:100%;">
<colgroup>
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
<col style="width: 11%" />
</colgroup>
<thead>
<tr>
<th style="text-align: center;"><a href="../../../docs/how_to/tool_calling">Tool calling</a></th>
<th style="text-align: center;"><a href="../../../docs/how_to/structured_output/">Structured output</a></th>
<th style="text-align: center;">JSON mode</th>
<th style="text-align: center;"><a href="../../../docs/how_to/multimodal_inputs/">Image input</a></th>
<th style="text-align: center;">Audio input</th>
<th style="text-align: center;">Video input</th>
<th style="text-align: center;"><a href="../../../docs/how_to/chat_streaming/">Token-level streaming</a></th>
<th style="text-align: center;"><a href="../../../docs/how_to/chat_token_usage_tracking/">Token usage</a></th>
<th style="text-align: center;"><a href="../../../docs/how_to/logprobs/">Logprobs</a></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">✅</td>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">❌</td>
<td style="text-align: center;">✅</td>
<td style="text-align: center;">✅</td>
<td style="text-align: center;">❌</td>
</tr>
</tbody>
</table>
`,
];

This guide will help you getting started with Perplexity [chat
models](../../../docs/concepts/#chat-models). For detailed documentation
of all `ChatPerplexity` features and configurations head to the [API
reference](https://api.js.langchain.com/classes/_langchain_community.chat_models_perplexity.ChatPerplexity.html).

## Overview

### Integration details

<div dangerouslySetInnerHTML={{ __html: quartoRawHtml[0] }} />

### Model features

See the links in the table headers below for guides on how to use
specific features.

<div dangerouslySetInnerHTML={{ __html: quartoRawHtml[1] }} />

Note that at the time of writing, Perplexity only supports structured
outputs on certain usage tiers.

## Setup

To access Perplexity models you’ll need to create a Perplexity account,
get an API key, and install the `@langchain/community` integration
package.

### Credentials

Head to https://perplexity.ai to sign up for Perplexity and generate an
API key. Once you’ve done this set the `PERPLEXITY_API_KEY` environment
variable:

```bash
export PERPLEXITY_API_KEY="your-api-key"
```

If you want to get automated tracing of your model calls you can also
set your [LangSmith](https://docs.smith.langchain.com/) API key by
uncommenting below:

```bash
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### Installation

The LangChain Perplexity integration lives in the `@langchain/community`
package:

```mdx-code-block
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
import Npm2Yarn from "@theme/Npm2Yarn";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

<Npm2Yarn>
  @langchain/community @langchain/core
</Npm2Yarn>

```

## Instantiation

Now we can instantiate our model object and generate chat completions:

```typescript
import { ChatPerplexity } from "@langchain/community/chat_models/perplexity";

const llm = new ChatPerplexity({
  model: "sonar",
  temperature: 0,
  maxTokens: undefined,
  timeout: undefined,
  maxRetries: 2,
  // other params...
});
```

## Invocation

```typescript
const aiMsg = await llm.invoke([
  {
    role: "system",
    content:
      "You are a helpful assistant that translates English to French. Translate the user sentence.",
  },
  {
    role: "user",
    content: "I love programming.",
  },
]);
aiMsg;
```

```text
AIMessage {
  "id": "run-71853938-aa30-4861-9019-f12323c09f9a",
  "content": "J'adore la programmation.",
  "additional_kwargs": {
    "citations": [
      "https://careersatagoda.com/blog/why-we-love-programming/",
      "https://henrikwarne.com/2012/06/02/why-i-love-coding/",
      "https://forum.freecodecamp.org/t/i-love-programming-but/497502",
      "https://ilovecoding.org",
      "https://thecodinglove.com"
    ]
  },
  "response_metadata": {
    "tokenUsage": {
      "promptTokens": 20,
      "completionTokens": 9,
      "totalTokens": 29
    }
  },
  "tool_calls": [],
  "invalid_tool_calls": []
}
```

```typescript
console.log(aiMsg.content);
```

```text
J'adore la programmation.
```

## Chaining

We can [chain](../../../docs/how_to/sequence/) our model with a prompt
template like so:

```typescript
import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "You are a helpful assistant that translates {input_language} to {output_language}.",
  ],
  ["human", "{input}"],
]);

const chain = prompt.pipe(llm);
await chain.invoke({
  input_language: "English",
  output_language: "German",
  input: "I love programming.",
});
```

```text
AIMessage {
  "id": "run-a44dc452-4a71-423d-a4ee-50a2d7c90abd",
  "content": "**English to German Translation:**\n\n\"I love programming\" translates to **\"Ich liebe das Programmieren.\"**\n\nIf you'd like to express your passion for programming in more detail, here are some additional translations:\n\n- **\"Programming is incredibly rewarding and fulfilling.\"** translates to **\"Das Programmieren ist unglaublich lohnend und erfüllend.\"**\n- **\"I enjoy solving problems through coding.\"** translates to **\"Ich genieße es, Probleme durch Codieren zu lösen.\"**\n- **\"I find the process of creating something from nothing very satisfying.\"** translates to **\"Ich finde den Prozess, etwas aus dem Nichts zu schaffen, sehr befriedigend.\"**",
  "additional_kwargs": {
    "citations": [
      "https://careersatagoda.com/blog/why-we-love-programming/",
      "https://henrikwarne.com/2012/06/02/why-i-love-coding/",
      "https://dev.to/dvddpl/coding-is-boring-why-do-you-love-coding-cl0",
      "https://forum.freecodecamp.org/t/i-love-programming-but/497502",
      "https://ilovecoding.org"
    ]
  },
  "response_metadata": {
    "tokenUsage": {
      "promptTokens": 15,
      "completionTokens": 149,
      "totalTokens": 164
    }
  },
  "tool_calls": [],
  "invalid_tool_calls": []
}
```

## API reference

For detailed documentation of all ChatPerplexity features and
configurations head to the API reference:
https://api.js.langchain.com/classes/\_langchain_community.chat_models_perplexity.ChatPerplexity.html
